$(function(){
	queryDictType();
	loadTable();
})

//选定词类
function targetType(dom){
	$('#click_type_code').val($(dom).parent('.pDict').attr('dict-code'));
	$('.word_mean').css('color','black');
	$(dom).css('color','red');
	loadTable();
}

//添加子类词典数据
function addChild(dom){
	var parent_code = $(dom).parent('.pDict').attr('dict-code');
	var parent_mean = $(dom).parent('.pDict').children('.word_mean').text();
	var parent_info = {
			parent_code:parent_code
			,parent_mean:parent_mean
		};
	layer.open({
		type : 2
		,title : '添加子类词典'
		,area : ['400px','450px']
		,offset : '50px'
		,fixed : false
		,content : 'addDict.html'
		,resize : false
		,success: function (layero, index) {
            // 获取子页面的iframe
            var iframe = window['layui-layer-iframe' + index];
            // 向子页面的全局函数child传参
            iframe.transmitParam(parent_info);
        }
	})
}

//删除词类
function deleteDictType(dom){
	layer.confirm('确定删除该词类?',{icon:3,title:'提示'},function(index){
		var word_code = $(dom).parent('.pDict').attr('dict-code');
		layer.load(2);
		deleteDict(word_code);
		queryDictType();
		loadTable();
	})
}

//修改词类
function updateDictType(dom){
	var parent_code = $(dom).parent('.pDict').attr('dict-code');
	var parent_mean = $(dom).parent('.pDict').children('.word_mean').text();
	parent_mean = parent_mean.substr(0,parent_mean.indexOf('('));
	
	var wordInfo = {
			word_code : parent_code
			,word_mean : parent_mean
	}
	
	layer.open({
		type : 2
		,title : '修改词类'
		,area : ['400px','450px']
		,offset : '50px'
		,fixed : false
		,content : 'updateDict.html'
		,resize : false
		,success: function (layero, index) {
            // 获取子页面的iframe
            var iframe = window['layui-layer-iframe' + index];
            // 向子页面的全局函数child传参
            iframe.transmitParam(wordInfo);
        }
	})
}

//删除词典数据
function deleteDict(word_code){
	$.ajax({
		type : 'get'
		,dataType : 'json'
		,url : access_url+'/system/systemmanage/deletedict'
		,async : false
		,data : {
			word_code : word_code
		}
		,success : function(data){
			layer.closeAll('loading');
			if(data.code != 200){
				layer.msg(data.msg, {icon: 5});
				return;
			}
			layer.alert(data.msg,{icon:6});
		}
	})
}

//左侧字典父类的搜索方法
$('.ul-search').on('change',function(){
	var targetStr = $('.ul-search').val();
	if(null==targetStr||targetStr.trim().length==0){
		$('.pDict').removeClass('showLi');
		$('.pDict').show();
		return;
	}
	var lis = $('.dictType')[0].children;
	$(lis).each(function(){
		if($(this).text().indexOf(targetStr)>-1){
			$(this).addClass("showLi");
		}
	})
	console.log('遍历完成');
	$('.pDict').hide();
	$('.showLi').show();
})

function queryDictType(){
	$.ajax({
		type : 'get'
		,dataType : 'json'
		,async : false
		,url : access_url+'/system/systemmanage/querydictlist'
		,data : {
			parent_code : 0
		}
		,success : function(data){
			console.log("data",data);
			var retlist = data.retlist;
			var len = retlist.length;
			
			var dictTypeHtml = '';
			var formatStr;
			var liHmltStr;
			var dictValue;
			var meanHtml;
			
			for (var i = 0; i < len; i++) {
				dictValue = retlist[i];
				
				if(0==i){
					$('#click_type_code').val(dictValue.word_code);
				}
				
				
				formatStr = '<li class="pDict"  dict-code="{word_code}">'
					+ '<span class="word_mean" onclick="targetType(this)">{word_mean}({word_code})</span>'
					+ '<a href="javascript:;" class="type_delete" onclick="deleteDictType(this)"><img alt="X" src="../../image/system/delete.png"></a>'
					+ '<a href="javascript:;" class="type_update" onclick="updateDictType(this)"><img alt="X" src="../../image/system/update.png"></a>'
					+ '<a href="javascript:;" class="add_child" onclick="addChild(this)"><img alt="X" src="../../image/system/add.png"></a>'
					+ '</li>';
				liHmltStr = formatStr.format(dictValue);
				
				dictTypeHtml += liHmltStr;
			}
			
			$('.dictType').html(dictTypeHtml);
			
			$('.word_mean:first').css('color','red');
		}
	})
}

var loadTable = function dictTable(){
		layui.use('table',function(){
			var layer = layui.layer,
				table = layui.table;
			
			table.render({
				elem : '#word_group'
				,height : 'full-10'
				,url : access_url+'/system/systemmanage/querydictforpage'
				,toolbar : true
				,title : '字典词组'
				,method:'post'
				,contentType : 'application/json'
				,where : {
					params : {
						parent_code : $('#click_type_code').val()
					}
				}
				,request: {
					pageName: 'pageNum' //页码的参数名称，默认：page
					,limitName: 'pageSize' //每页数据量的参数名，默认：limit
				}
				,response: {
					statusCode: 200 //成功的状态码，默认：0
                }
				, page: {//开启分页
					layout: ['prev', 'page', 'next', 'skip', 'count' ,'limit'] //自定义分页布局
					,groups: 4 //只显示 4 个连续页码
					, prev:"上一页"
					, next:"下一页"
					, first : true
					, last : true
				}
				, limit: 10 //每页默认显示的数量
				, cols: [[ //表头
					{type:'checkbox'},
					{field: 'id', title: '序号',unresize:true,type:'numbers',width:50},
                    {
                        field: 'word_mean',
                        title: '词义',
                        align: 'center'
                    },
                    {
                    	field: 'word_code',
                    	title: '词码',
                    	align: 'center',
                    	sort: true
                    },
                    {
                    	field: 'word_value',
                    	title: '词值',
                    	align: 'center',
                    	sort: true
                    },
                    {
                    	field: 'create_time',
                    	title: '创建时间',
                    	align: 'center',
                    	sort: true
                    },
                    {
                    	field: 'update_time',
                    	title: '修改时间',
                    	align: 'center',
                    	sort: true
                    },
					{field: '', title: '操作',unresize:true,align: 'center',width:80,toolbar: '#operate'}
				]]
				,parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    console.log("parseData->res",res);
                    return {
                      "code": res.code, //解析接口状态
                      "msg": res.msg, //解析提示文本
                      "count": res.retdata.total, //解析数据长度
                      "data": res.retdata.pageData //解析数据列表
                    };
                }
			})
			
			table.on('tool(word_group)',function(obj){
				var data = obj.data;
				var layEvent = obj.event;
				var word_code = data.word_code;
				var parent_code = data.parent_code;
				var word_mean = data.word_mean;
				var word_value = data.word_value;
				var parent_mean = data.parent_mean;
				
				var wordInfo = {
						word_code : word_code
						,parent_code : parent_code
						,word_mean : word_mean
						,word_value : word_value
						,parent_mean : parent_mean
				}
				
				if('edit'==layEvent) {
					layer.open({
						type : 2
						,title : '编辑角色信息'
						,area : ['400px','450px']
						,offset : '50px'
						,fixed : false
						,content : 'updateDict.html'
						,resize : false
						,success: function (layero, index) {
		                    // 获取子页面的iframe
		                    var iframe = window['layui-layer-iframe' + index];
		                    // 向子页面的全局函数child传参
		                    iframe.transmitParam(wordInfo);
		                }
					})
				} else if ('del'==layEvent) {
					layer.confirm('确定删除该数据?',{icon:3,title:'提示'},function(index){
						layer.load(2);
						deleteDict(word_code);
						layer.close(index);
						loadTable();
					})
				}
			})
		})
	}

$('#add-dictType').click(function(){
	layer.open({
		type : 2
		,title : '添加词类'
		,area : ['400px','450px']
		,offset : '50px'
		,fixed : false
		,content : 'addDict.html'
		,resize : false
	})
})
